#ifndef MONO_COMMON_DATETIME_H_
#define MONO_COMMON_DATETIME_H_

#include <mono/common/timestamp.h>
#include <mono/common/timespan.h>

namespace mono {

class  DateTime {
public:
	/*!
	* Symbolic names for month numbers (1 to 12).
	*/
	enum Months {
		JANUARY = 1,
		FEBRUARY,
		MARCH,
		APRIL,
		MAY,
		JUNE,
		JULY,
		AUGUST,
		SEPTEMBER,
		OCTOBER,
		NOVEMBER,
		DECEMBER
	};

	/*!
	* Symbolic names for week day numbers (0 to 6).
	*/
	enum DaysOfWeek {
		SUNDAY = 0,
		MONDAY,
		TUESDAY,
		WEDNESDAY,
		THURSDAY,
		FRIDAY,
		SATURDAY
	};
	
	/*!
	* Creates a DateTime for the current date and time.
	*/	
	DateTime();
		

	/*!
	* Creates a DateTime for the date and time given in
	* a Timestamp.
	*/
	DateTime(const Timestamp& timestamp);
	


	/*!
	* Creates a DateTime for the given Gregorian date and time.
	*   * year is from 0 to 9999.
	*   * month is from 1 to 12.
	*   * day is from 1 to 31.
	*   * hour is from 0 to 23.
	*   * minute is from 0 to 59.
	*   * second is from 0 to 59.
	*   * millisecond is from 0 to 999.
	*   * microsecond is from 0 to 999.
	*/	
	DateTime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);

	/*!
	*   Creates a DateTime for the given Julian day.
	*/	
	DateTime(double julianDay);

	/*!
	* Creates a DateTime from an UtcTimeVal and a TimeDiff.
	*
	* Mainly used internally by DateTime and friends.	
	*/	
	DateTime(Timestamp::UtcTimeVal utcTime, Timestamp::TimeDiff diff);

		
	/*!
	* Copy constructor. Creates the DateTime from another one.
	*/	
	DateTime(const DateTime& dateTime);

	/*!
	* Destroys the DateTime.
	*/	
	~DateTime();
	
	/*!
	* Assigns another DateTime.
	*/	
	DateTime& operator = (const DateTime& dateTime);
	
	/*!
	* Assigns a Timestamp.
	*/	
	DateTime& operator = (const Timestamp& timestamp);
	
	/*!
	* Assigns a Julian day.
	*/	
	DateTime& operator = (double julianDay);
		
	/*!
	* Assigns a Gregorian date and time.
	*   * year is from 0 to 9999.
	*   * month is from 1 to 12.
	*   * day is from 1 to 31.
	*   * hour is from 0 to 23.
	*   * minute is from 0 to 59.
	*   * second is from 0 to 59.
	*   * millisecond is from 0 to 999.
	*   * microsecond is from 0 to 999.
	*/	
	DateTime& assign(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microseconds = 0);

	
	/*!
	* Swaps the DateTime with another one.
	*/	
	void swap(DateTime& dateTime);
		

	/*!
	* @return the year.
	*/	
	int year() const;
		
	/*!
	* @return the month (1 to 12).
	*/		
	int month() const;
	
	/*!
	* @return the week number within the year.
	* FirstDayOfWeek should be either SUNDAY (0) or MONDAY (1).
	* The returned week number will be from 0 to 53. Week number 1 is the week 
	* containing January 4. This is in accordance to ISO 8601.
	* 
	* The following example assumes that firstDayOfWeek is MONDAY. For 2005, which started
	* on a Saturday, week 1 will be the week starting on Monday, January 3.
	* January 1 and 2 will fall within week 0 (or the last week of the previous year).
	*
	* For 2007, which starts on a Monday, week 1 will be the week starting on Monday, January 1.
	* There will be no week 0 in 2007.
	*/	
	int week(int firstDayOfWeek = MONDAY) const;

	/*!
	* @return the day within the month (1 to 31).
	*/
	int day() const;
		
	/*!
	* @return the weekday (0 to 6, where
	* 0 = Sunday, 1 = Monday, ..., 6 = Saturday).	
	*/
	int dayOfWeek() const;
		
	/*!
	* @return  the number of the day in the year.
	* January 1 is 1, February 1 is 32, etc.
	*/
	int dayOfYear() const;

	/*!
	* @return  the hour (0 to 23).
	*/
	int hour() const;
	
	/*!
	* @return  the hour (0 to 12).
	*/	
	int hourAMPM() const;

	/*!
	* @return  the hour (0 to 12).
	*/	
	bool isAM() const;

	/*!
	* @return  true if hour >= 12.
	*/	
	bool isPM() const;

	/*!
	* @return  the minute (0 to 59).
	*/	
	int minute() const;
		
	/*!
	* @return  the second (0 to 59).	
	*/	
	int second() const;

	/*!
	* @return  the millisecond (0 to 999)
	*/	
	int millisecond() const;
		
	/*!
	* @return the microsecond (0 to 999)
	*/	
	int microsecond() const;
		
	/*!
	* @return the julian day for the date and time.
	*/
	double julianDay() const;
		
	/*!
	* @return the date and time expressed as a Timestamp.	
	*/
	Timestamp timestamp() const;
		

	/*!
	* @return the date and time expressed in UTC-based
	* time. UTC base time is midnight, October 15, 1582.
	* Resolution is 100 nanoseconds.	
	*/
	Timestamp::UtcTimeVal utcTime() const;


	bool operator == (const DateTime& dateTime) const;
	bool operator != (const DateTime& dateTime) const;
	bool operator <  (const DateTime& dateTime) const;
	bool operator <= (const DateTime& dateTime) const;
	bool operator >  (const DateTime& dateTime) const;
	bool operator >= (const DateTime& dateTime) const;

	DateTime  operator +  (const Timespan& span) const;
	DateTime  operator -  (const Timespan& span) const;
	Timespan  operator -  (const DateTime& dateTime) const;
	DateTime& operator += (const Timespan& span);
	DateTime& operator -= (const Timespan& span);

	/*!
	* Converts a local time into UTC, by applying the given time zone differential.	
	*/
	void makeUTC(int tzd);

	/*!
	* Converts a UTC time into a local time, by applying the given time zone differential.
	*/
	void makeLocal(int tzd);
		

	/*!
	* @return true if the given year is a leap year;
	* false otherwise.
	*/
	static bool isLeapYear(int year);

	/*!
	* @return the number of days in the given month
	* and year. Month is from 1 to 12.
	*/
	static int daysOfMonth(int year, int month);

	/*!
	* Checks if the given date and time is valid
	* (all arguments are within a proper range).
	*
	* Returns true if all arguments are valid, false otherwise.
	*/
	static bool isValid(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);


protected:

	/*!
	* Computes the Julian day for an UTC time.
	*/
	static double toJulianDay(Timestamp::UtcTimeVal utcTime);
		
	/*!
	* Computes the Julian day for a Gregorian calendar date and time.
	* See <http://vsg.cape.com/~pbaum/date/jdimp.htm>, section 2.3.1 for the algorithm.
	*/
	static double toJulianDay(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
			
	/*!
	* Computes the UTC time for a Julian day.
	*/
	static Timestamp::UtcTimeVal toUtcTime(double julianDay);
	
	/*!
	*  Computes the Gregorian date for the given Julian day.
	* See <http://vsg.cape.com/~pbaum/date/injdimp.htm>, section 3.3.1 for the algorithm.
	*/
	void computeGregorian(double julianDay);

	/*!
	* Extracts the daytime (hours, minutes, seconds, etc.) from the stored utcTime.
	*/
	void computeDaytime();
	
private:
	void checkLimit(short& lower, short& higher, short limit);
	
	/*!
	* utility functions used to correct the overflow in computeGregorian
	*/
	void normalize();
		

	Timestamp::UtcTimeVal _utcTime;
	short  _year;
	short  _month;
	short  _day;
	short  _hour;
	short  _minute;
	short  _second;
	short  _millisecond;
	short  _microsecond;
};


//
// inlines
//
inline Timestamp DateTime::timestamp() const
{
	return Timestamp::fromUtcTime(_utcTime);
}


inline Timestamp::UtcTimeVal DateTime::utcTime() const
{
	return _utcTime;
}


inline int DateTime::year() const
{
	return _year;
}

	
inline int DateTime::month() const
{
	return _month;
}

	
inline int DateTime::day() const
{
	return _day;
}

	
inline int DateTime::hour() const
{
	return _hour;
}


inline int DateTime::hourAMPM() const
{
	if (_hour < 1)
		return 12;
	else if (_hour > 12)
		return _hour - 12;
	else
		return _hour;
}


inline bool DateTime::isAM() const
{
	return _hour < 12;
}


inline bool DateTime::isPM() const
{
	return _hour >= 12;
}

	
inline int DateTime::minute() const
{
	return _minute;
}

	
inline int DateTime::second() const
{
	return _second;
}

	
inline int DateTime::millisecond() const
{
	return _millisecond;
}


inline int DateTime::microsecond() const
{
	return _microsecond;
}


inline bool DateTime::operator == (const DateTime& dateTime) const
{
	return _utcTime == dateTime._utcTime;
}


inline bool DateTime::operator != (const DateTime& dateTime) const
{
	return _utcTime != dateTime._utcTime;
}


inline bool DateTime::operator < (const DateTime& dateTime) const
{
	return _utcTime < dateTime._utcTime;
}


inline bool DateTime::operator <= (const DateTime& dateTime) const
{
	return _utcTime <= dateTime._utcTime;
}


inline bool DateTime::operator > (const DateTime& dateTime) const
{
	return _utcTime > dateTime._utcTime;
}


inline bool DateTime::operator >= (const DateTime& dateTime) const
{
	return _utcTime >= dateTime._utcTime;
}


inline bool DateTime::isLeapYear(int year)
{
	return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
}


inline void swap(DateTime& d1, DateTime& d2)
{
	d1.swap(d2);
}


} 


#endif
